package com.zmn.plat.admin.controller.warranty.content;

import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.google.common.collect.Maps;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.KvDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.office.excel.ExportExcel;
import com.zmn.plat.business.interfaces.product.ServProductBService;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.common.dictionary.ProductDict;
import com.zmn.plat.model.entity.category.BaseCategory;
import com.zmn.plat.model.entity.category.BaseCategoryQuery;
import com.zmn.plat.model.entity.servcategory.ServAssCategoryQuery;
import com.zmn.plat.model.entity.servcategory.ServCategory;
import com.zmn.plat.model.entity.warranty.content.WarrantyContent;
import com.zmn.plat.model.entity.warranty.content.WarrantyContentQuery;
import com.zmn.plat.model.vo.warranty.WarrantyContentVO;
import com.zmn.plat.services.interfaces.category.BaseCategoryService;
import com.zmn.plat.services.interfaces.servcategory.ServAssCategoryService;
import com.zmn.plat.services.interfaces.servcategory.ServCategoryService;

/**
 * 保修内容配置
 *
 * @author linfeng
 * @since 2020-06-08 10:37:02
 */
@Controller
@RequestMapping("/warranty/content/")
public class WarrantyContentListController extends WarrantyContentBaseController {

    @Resource
    protected ServCategoryService servCategoryService;

    @Resource
    protected ServAssCategoryService servAssCategoryService;

    @Resource
    protected BaseCategoryService baseCategoryService;

    @Resource
    protected ServProductBService servProductBService;

    /**
     * 列表
     *
     * @param servCategId
     * @param categOneId
     * @since 2020-06-08 10:37:02
     */
    @RequestMapping("listByCateg")
    // @RequiresPermissions("/warranty/content/list.action")
    public ModelAndView listByCateg(@RequestParam(value = "servCategId", defaultValue = "0") Integer servCategId,
        @RequestParam(value = "categOneId", defaultValue = "0") Integer categOneId,
        @RequestParam(value = "bizType", defaultValue = "0") String bizType) {
        WarrantyContentQuery query = new WarrantyContentQuery();
        query.setServCategId(servCategId);
        query.setCategOneId(categOneId);
        query.setBizType(bizType);
        return list(query);
    }

    /**
     * 列表
     *
     * @param query
     * @author linfeng
     * @since 2020-06-08 10:37:02
     */
    @RequestMapping("list")
    // @RequiresPermissions("/warranty/content/list.action")
    public ModelAndView list(@ModelAttribute WarrantyContentQuery query) {

        if (query.getStatus() == null) {
            query.setStatus(GlobalConsts.YES);
        }
        List<WarrantyContent> items = warrantyContentService.listPageByQuery(query);

        ServCategory querySC = new ServCategory();
        querySC.setStatus(GlobalConsts.YES);
        List<ServCategory> servCategories = servCategoryService.listByQuery(querySC);
        List<KvDTO<Integer, String>> baseCategories = Collections.emptyList();

        if (NumberUtil.isNotNullOrZero(query.getServCategId())) {
            ServAssCategoryQuery queryBQ = new ServAssCategoryQuery();
            queryBQ.setServCategId(query.getServCategId());
            queryBQ.setCategOneStatus(GlobalConsts.YES);
            queryBQ.setShowType(ProductConsts.ERP_PRODUCT_TYPE);
            baseCategories = servAssCategoryService.listCateOneByQuery(queryBQ);
        }

        BaseCategoryQuery queryB = new BaseCategoryQuery();
        List<BaseCategory> baseCategorieall = baseCategoryService.listByQuery(queryB);

        WarrantyContent warrantyContent;
        for (int i = 0; i < items.size(); i++) {
            warrantyContent = items.get(i);
            warrantyContent.setServCategName(servProductBService.findServCategNameById(servCategories, warrantyContent.getServCategId(),
                ProductConsts.ERP_PRODUCT_TYPE));
            warrantyContent.setCategOneName(servProductBService.findBaseCategNameById(baseCategorieall, warrantyContent.getCategOneId()));
        }

        ModelAndView mav = new ModelAndView("warranty/content/list");
        mav.addObject("items", items);
        mav.addObject("vo", query);
        mav.addObject("servCategories", servCategories);
        mav.addObject("baseCategories", baseCategories);
        mav.addObject("bizTypes", ProductDict.listBizType());
        return mav;
    }

    /**
     * 详情
     *
     * @param warrantyContentId
     * @author linfeng
     * @since 2020-06-08 10:37:02
     */
    @RequestMapping("detail")
    // @RequiresPermissions("/warranty/content/detail.action")
    public ModelAndView detail(@RequestParam(value = "warrantyContentId", defaultValue = "0") Integer warrantyContentId) {
        WarrantyContent warrantyContent;
        if (warrantyContentId == 0) {
            warrantyContent = new WarrantyContent();
        } else {
            warrantyContent = warrantyContentService.findByKey(warrantyContentId);
        }

        ServCategory querySC = new ServCategory();
        querySC.setStatus(GlobalConsts.YES);
        List<ServCategory> servCategories = servCategoryService.listByQuery(querySC);
        List<KvDTO<Integer, String>> baseCategories = Collections.emptyList();

        if (NumberUtil.isNotNullOrZero(warrantyContent.getServCategId())) {
            ServAssCategoryQuery queryBQ = new ServAssCategoryQuery();
            queryBQ.setServCategId(warrantyContent.getServCategId());
            queryBQ.setCategOneStatus(GlobalConsts.YES);
            queryBQ.setShowType(ProductConsts.ERP_PRODUCT_TYPE);
            baseCategories = servAssCategoryService.listCateOneByQuery(queryBQ);
        }

        ModelAndView mav = new ModelAndView("warranty/content/detail");
        mav.addObject("warrantyContent", warrantyContent);
        mav.addObject("servCategories", servCategories);
        mav.addObject("baseCategories", baseCategories);
        return mav;
    }

    @RequestMapping(value = "exportExcel")
    public void exportExcel(HttpServletResponse response, WarrantyContentQuery query) {
        Integer pageSize = getExportPageLimit(response);
        query.setPageSize(pageSize);

        List<WarrantyContentVO> warrantyContentVOS = warrantyContentService.listExportExcelPageByQuery(query);
        if (CollectionUtils.isEmpty(warrantyContentVOS)) {
            warrantyContentVOS = new ArrayList<>();
            warrantyContentVOS.add(new WarrantyContentVO());
        } else {
            warrantyContentVOS.forEach(e -> {
                e.setUpdateTimeAndUpdater(DateUtil.toString(e.getUpdateTime()) + "/" + e.getUpdater());
                e.setStatusName(Objects.equals(GlobalConsts.YES, e.getStatus()) ? "启用" : "停用");
                e.setBizTypeName(ProductDict.translateBizType(e.getBizType(), null));
            });
        }

        // 封装Excel对象
        LinkedHashMap<String, String> fieldMap = Maps.newLinkedHashMap();
        fieldMap.put("sort", "排序号");
        fieldMap.put("warrantyContentId", "ID");
        fieldMap.put("content", "保修内容");
        fieldMap.put("bizTypeName", "业务类型");
        fieldMap.put("servCategName", "服务分类");
        fieldMap.put("categOneName", "一级产品分类");
        fieldMap.put("statusName", "状态");
        fieldMap.put("updateTimeAndUpdater", "最后一次操作时间/人");

        try {
            ExportExcel<WarrantyContentVO> excel = new ExportExcel<>(warrantyContentVOS, fieldMap, true);
            excel.exportExcel(response, "保修内容");
        } catch (Exception e) {
            logger.error("导出报表出错" + e.getMessage(), e);
        }
    }

}
